package com.qianguo.buyer.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianguo.buyer.R;

/**
 * 购物车数量加减选择控件
 * Created by guxun on 2016-05-12.
 */
public class ShopCarChooseView extends LinearLayout {
    /**
     * 隐藏软键盘
     */
    private InputMethodManager mInputManager;
    private int MAX_NUMBER = 100;//最大值
    private int MIN_NUMBER = 0;//最小值
    private int mTotalNum = 0;//实际选择值
    private String mContent = "";
    FrameLayout mFlipperView;
    TextView mPrevTextView;//轮播区上一个文字
    TextView mNextTextView;//轮播区下一个文字

    Animation mFromTopAnimation;
    Animation mFromBottomAnimation;
    Animation mToTopAnimation;
    Animation mToBottomAnimation;
    TextView mAddBt,mStractBt;
    private Context mContext;
    OnChooseClickListener mChooseClickListener;
    public int getMAX_NUMBER() {
        return MAX_NUMBER;
    }

    public void setMAX_NUMBER(int MAX_NUMBER) {
        this.MAX_NUMBER = MAX_NUMBER;
    }

    public int getMIN_NUMBER() {
        return MIN_NUMBER;
    }

    public void setMIN_NUMBER(int MIN_NUMBER) {
        this.MIN_NUMBER = MIN_NUMBER;
    }

    public String getmContent() {
        return mContent;
    }

    public void setmContent(String mContent) {
        this.mContent = mContent;
    }

    public ShopCarChooseView(Context context) {
        super(context);
        init(context);
    }

    public ShopCarChooseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ShopCarChooseView);
        mContent = typedArray.getString(R.styleable.ShopCarChooseView_content);
        MAX_NUMBER = typedArray.getInteger(R.styleable.ShopCarChooseView_maxNum, MAX_NUMBER);
        MIN_NUMBER = typedArray.getInteger(R.styleable.ShopCarChooseView_minNum, 0);
        typedArray.recycle();
        mTotalNum = MIN_NUMBER;
        init(context);
    }

    private void init(final Context context) {
        mInputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
        initMinusView(context);
        initFlipperView(context);
        initPlusView(context);
    }

    private void initPlusView(final Context context) {
        mAddBt = new TextView(context);
        mAddBt.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector_publish_button_normal_pressed));
        mAddBt.setGravity(Gravity.CENTER);
        LayoutParams mAddBtParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mAddBt.setText("+");
        mAddBt.setTextSize(18);
        mAddBt.setTextColor(getResources().getColor(R.color.white));
        addView(mAddBt, mAddBtParams);
        mAddBt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTotalNum < MAX_NUMBER) {
                    showFlipperNextTextView();

                    if(mChooseClickListener!=null){
                        mChooseClickListener.onItemAddClickListener(mTotalNum);
                    }
                } else {
                    mAddBt.setEnabled(false);
//                    Toast.makeText(context, "不能选择更多了哦。", Toast.LENGTH_SHORT).show();
                }
            }

        });
    }

    private void initMinusView(Context context) {
        mStractBt = new TextView(context);
        mStractBt.setText("―");
        mStractBt.setTextSize(18);
        mStractBt.setTextColor(getResources().getColor(R.color.white));
        mStractBt.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector_publish_button_normal_pressed));
        mStractBt.setGravity(Gravity.CENTER);
        LayoutParams mStractBtParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        addView(mStractBt, mStractBtParams);
        mStractBt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mAddBt.setEnabled(true);
                if (mTotalNum > MIN_NUMBER) {
                    showFlipperPrevTextView();
                    if(mChooseClickListener!=null){
                        mChooseClickListener.onItemMinusClickListener(mTotalNum);
                    }
                }
            }
        });
    }

    /**
     * 初始化轮播器
     */
    private void initFlipperView(Context context) {
        mFlipperView = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.view_textview_flipper, null);
        mPrevTextView = (TextView) mFlipperView.findViewById(R.id.prev_tv);
        mNextTextView = (TextView) mFlipperView.findViewById(R.id.next_tv);
        mNextTextView.setVisibility(View.GONE);
        LayoutParams mShowTvBtParams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
        mShowTvBtParams.weight = 1;
        mFlipperView.setLayoutParams(mShowTvBtParams);
        addView(mFlipperView);
        mNextTextView.setText(String.valueOf(mTotalNum + 1) + mContent);
        mPrevTextView.setText(String.valueOf(mTotalNum) + mContent);
        mFromTopAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_from_top_counter);
        mFromBottomAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_from_bottom_counter);
        mToTopAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_to_top_counter);
        mToBottomAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_to_bottom_counter);
    }

    /**
     * 显示下一个
     */
    private synchronized void showFlipperNextTextView() {
        mTotalNum++;
        mNextTextView.setText(String.valueOf(mTotalNum) + mContent);
        mPrevTextView.setText(String.valueOf(mTotalNum - 1) + mContent);
        mNextTextView.setVisibility(View.VISIBLE);
        mPrevTextView.setVisibility(View.VISIBLE);
        mNextTextView.startAnimation(mFromTopAnimation);
        mPrevTextView.startAnimation(mToBottomAnimation);
        mPrevTextView.setVisibility(View.GONE);
    }

    /**
     * 显示上一个
     */
    private synchronized void showFlipperPrevTextView() {
        mTotalNum--;
        mNextTextView.setText(String.valueOf(mTotalNum + 1) + mContent);
        mPrevTextView.setText(String.valueOf(mTotalNum) + mContent);
        mPrevTextView.setVisibility(View.VISIBLE);
        mNextTextView.setVisibility(View.VISIBLE);
        mPrevTextView.startAnimation(mFromBottomAnimation);
        mNextTextView.startAnimation(mToTopAnimation);
        mNextTextView.setVisibility(View.GONE);
    }

    public void setOnChooseClickListener(OnChooseClickListener lis){
        this.mChooseClickListener = lis;
    }
    /**
     * 获取当前选中的值
     *
     * @return
     */
    public synchronized int getNumber() {
        return mTotalNum;
    }

    public synchronized void setNumber(int num) {
        mTotalNum = num - 1;
        showFlipperNextTextView();
    }

    public interface OnChooseClickListener {

        void onItemAddClickListener(int value);

        void onItemMinusClickListener(int value);
    }

}
